package com.reader.spider;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

public class SevenKanKanProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(100);


    @Override
    public void process(Page page) {
        page.addTargetRequests(page.getHtml().links().regex("(http://m.7kankan.com/[0-9]+.html)").all());
        page.addTargetRequests(page.getHtml().links().regex("(http://m.7kankan.com/xiaoshuotopallvote/[0-9]+/[0-9]+.html)").all());
        page.putField("url", page.getUrl().regex("(http://m.7kankan.com/[0-9]+.html)").toString());
        page.putField("name", page.getHtml().xpath("//div[@class='block']/div[@class='block_txt2']/h2/a/text()").toString());
        if (page.getResultItems().get("name")==null || page.getResultItems().get("url")==null){
            //skip this page
            page.setSkip(true);
        }
        page.putField("intoduceInfo", page.getHtml().xpath("//div[@class='cover']/div[@class='intro_info']/tidyText()"));
        page.putField("catalogueUrl", page.getHtml().links().regex("(http://m.7kankan.com/html/[0-9]+/[0-9]+/index.html)").all());
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider spider = Spider.create(new SevenKanKanProcessor());
        spider.clearPipeline();
        spider.addUrl("http://m.7kankan.com/xiaoshuotopallvote/0/1.html").addPipeline(new SevenKanKanDBPipeline()).thread(5).run();
    }
}
